package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.dto.NewsDto;
import com.xueyi.aixihu.entity.po.*;
import com.xueyi.aixihu.entity.vo.ArticleVo;
import com.xueyi.aixihu.entity.vo.CdzSystemType;
import com.xueyi.aixihu.entity.vo.DtNewsVo;
import com.xueyi.aixihu.entity.vo.WxArticleVo;
import com.xueyi.aixihu.reqeust.*;
import com.xueyi.aixihu.response.ArticleListResponse;
import com.xueyi.aixihu.response.RemoveArticleAreaReq;
import org.apache.catalina.util.Introspection;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @authoer:hxs
 * @createDate:2022/8/16
 * @description: 文章操作mapper
 */
public interface LocalDtNewsMapper {
    /**
     * 查询三级分类文章
     *
     * @param userManageAreaId
     * @param page
     * @param limit
     * @return
     */
    @Select({
            "<script> select dn.id,  " +
                    "       dn.cid,  " +
                    "       dn.title,  " +
                    "       dn.content,  " +
                    "       dn.create_time_new,  " +
                    "       dn.status,  " +
                    "       dn.paixu,  " +
                    "       dn.from_to_area_id," +
                    "       dn.img," +
                    "       dn.new_type, " +
                    "       dns.name classifyName," +
                    "       dn.article_type " +
                    " from (select * from dt_news " +
                    "where cid in ( ${ids} )  " +
//                    " and new_type = 1 " +
                    " and is_valid = 1  " +
                    "  and cid is not null " +
                    " <if test = 'title != null'>" +
                    " and title like '%${title}%' " +
                    " </if>" +
                    " <if test = 'status != null'>" +
                    " and status = #{status} " +
                    " </if>" +
                    " ) dn left join dt_news_sort dns on dn.cid = dns.id  " +

//                    "  and from_to_area_id in (${userManageAreaId})  " +
                    "ORDER BY order by cast( dn.paixu AS DECIMAL ) DESC  " +
                    "LIMIT #{page}, #{limit} </script>"
    })
    List<DtNewsVo> findArticleList(@Param("ids") String ids, @Param("userManageAreaId") String userManageAreaId,
                                   @Param("page") Integer page, @Param("limit") Integer limit,
                                   @Param("title") String title, @Param("status") Integer status);

    /**
     * 查询三级分类文章
     *
     * @return
     */
    @Select({
            " <script> select count(id)  " +
                    "from dt_news  " +
                    "where cid in (${ids})  " +
//                    " and new_type = 1 " +
                    " and is_valid = 1  " +
                    "  and cid is not null " +
                    " <if test = 'title != null'>" +
                    " and title like '%${title}%' " +
                    " </if>" +
                    " <if test = 'status != null'>" +
                    " and status = #{status} " +
                    " </if>" +
                    " </script>"
    })
    Integer findArticleListTotal(@Param("ids") String ids,
                                 @Param("title") String title, @Param("status") Integer status);


    /**
     * 查询二级分类文章
     *
     * @param userManageAreaId
     * @param page
     * @param limit
     * @return
     */
    @Select({
            "<script> select dn.id,  " +
                    "       dn.cid,  " +
                    "       dn.title,  " +
                    "       dn.content,  " +
                    "       dn.create_time_new,  " +
                    "       dn.status,  " +
                    "       dn.paixu,  " +
                    "       dn.from_to_area_id," +
                    "       dn.img," +
                    "       dn.new_type, " +
                    "       dns.name classifyName," +
                    "       dn.article_type  " +
                    " from (select * from dt_news " +
                    " where cid in (${ids})  " +
//                    " and new_type = 1 " +
                    " and is_valid = 1  " +
                    "  and cid is not null " +
                    " <if test = 'title != null'>" +
                    " and title like '%${title}%' " +
                    " </if>" +
                    " <if test = 'status != null'>" +
                    " and status = #{status} " +
                    " </if>" +
                    ") dn left join dt_news_sort dns on dn.cid = dns.id  " +

//                    "  AND from_to_area_id IN (${userManageAreaId})  " +
                    "order by cast( dn.paixu AS DECIMAL ) DESC  " +
                    "LIMIT #{page}, #{limit} </script>"
    })
    List<DtNewsVo> findTwoArticleList(@Param("ids") String ids, @Param("userManageAreaId") String userManageAreaId,
                                      @Param("page") Integer page, @Param("limit") Integer limit,
                                      @Param("title") String title, @Param("status") Integer status);

    /**
     * 查询二级分类文章
     * <p>
     * //     * @param threeClassifyId
     *
     * @return
     */
    @Select({
            " <script> select count(id)  " +
                    "from dt_news  " +
                    "where cid in (${ids}) " +
//                    " and new_type = 1 " +
                    " and is_valid = 1  " +
                    "  and cid is not null " +
                    " <if test = 'title != null'>" +
                    " and title like '%${title}%' " +
                    " </if>" +
                    " <if test = 'status != null'>" +
                    " and status = #{status} " +
                    " </if>" +
                    "</script>"
    })
    Integer findTwoArticleListTotal(@Param("ids") String ids,
                                    @Param("title") String title, @Param("status") Integer status);


    /**
     * 查询一级分类菜单
     *
     * @param userManageAreaId
     * @param page
     * @param limit
     * @return
     */
    @Select({
            "<script>select dn.id,  " +
                    "       dn.cid,  " +
                    "       dn.title,  " +
                    "       dn.content,  " +
                    "       dn.create_time_new,  " +
                    "       dn.status,  " +
                    "       dn.paixu,  " +
                    "       dn.from_to_area_id," +
                    "       dn.img," +
                    "       dn.new_type, " +
                    "       dns.name classifyName," +
                    "       dn.article_type  " +
                    "from (select * from dt_news " +
                    "where cid in (${ids})  " +
//                    " and new_type = 1 " +
                    " and is_valid = 1  " +
                    "  and cid is not null " +
                    " <if test = 'title != null'>" +
                    " and title like '%${title}%' " +
                    " </if>" +
                    " <if test = 'status != null'>" +
                    " and status = #{status} " +
                    " </if>" +
                    ") dn left join dt_news_sort dns on dn.cid = dns.id  " +

//                    "  and from_to_area_id in (${userManageAreaId})  " +
                    "  " +
                    "order by cast( dn.paixu AS DECIMAL ) DESC  " +
                    "limit #{page},#{limit} </script>"
    })
    List<DtNewsVo> findOneArticleList(@Param("ids") String ids, @Param("userManageAreaId") String userManageAreaId,
                                      @Param("page") Integer page, @Param("limit") Integer limit,
                                      @Param("title") String title, @Param("status") Integer status);

    /**
     * 查询一级分类文章总条数
     *
     * @return
     */
    @Select({
            " <script>select count(id)  " +
                    "from dt_news  " +
                    "where cid in (${ids})  " +
//                    " and new_type = 1 " +
                    " and is_valid = 1  " +
                    "  and cid is not null " +
                    " <if test = 'title != null'>" +
                    " and title like '%${title}%' " +
                    " </if> " +
                    " <if test = 'status != null'>" +
                    " and status = #{status} " +
                    " </if>" +
                    "</script>"
    })
    Integer findOneArticleListTotal(@Param("ids") String ids,
                                    @Param("title") String title, @Param("status") Integer status);


    /**
     * 查询所有的文章
     *
     * @param userManageAreaId
     * @param page
     * @param limit
     * @return
     */
    @Select({
            "<script> select dn.id,  " +
                    "       dn.cid,  " +
                    "       dn.title,  " +
                    "       dn.content,  " +
                    "       dn.create_time_new,  " +
                    "       dn.status,  " +
                    "       dn.paixu,  " +
                    "       dn.from_to_area_id," +
                    "       dn.img," +
                    "       dn.new_type, " +
                    "       dns.name classifyName," +
                    "       dn.article_type  " +
                    "from (select * from dt_news " +
                    " where  " +
//                    " new_type = 1 " +
                    "  is_valid = 1 " +
                    "  and cid is not null " +
                    " <if test = 'title != null'>" +
                    " and title like '%${title}%' " +
                    " </if>" +
                    " <if test = 'status != null'>" +
                    " and status = #{status} " +
                    " </if>" +
                    " ) dn left join dt_news_sort dns on dn.cid = dns.id " +

                    "order by cast( dn.paixu AS DECIMAL ) desc  " +
                    "limit #{page},#{limit} </script> "
    })
    List<DtNewsVo> findAllArticleList(@Param("userManageAreaId") String userManageAreaId,
                                      @Param("page") Integer page, @Param("limit") Integer limit,
                                      @Param("title") String title, @Param("status") Integer status);

    /**
     * 查询所有的文章总条数
     *
     * @return
     */
    @Select({
            "<script> select count(id)  " +
                    "from dt_news where  " +
//                    " new_type = 1 " +
                    "  is_valid = 1  " +
                    "  and cid is not null " +
                    " <if test = 'title != null'>" +
                    " and title like '%${title}%' " +
                    " </if>" +
                    " <if test = 'status != null'>" +
                    " and status = #{status} " +
                    " </if>" +
                    " </script>"
    })
    Integer findAllArticleListTotal(@Param("title") String title, @Param("status") Integer status);


    /**
     * 通过文章id查询文章详细信息
     *
     * @param id
     * @return
     */
    @Select({
            " select * from dt_news where id = #{id} and is_valid = 1 "
    })
    DtNewsVo findDtNewsById(@Param("id") Integer id);

    @Select({
            "<script> ",
            "select * from dt_users where id = #{userId}",
            "</script> ",
    })
    DtUsers selectUserById(@Param("userId") Integer userId);

    //查询文章对应的审核记录信息
    @Select({
            "<script> ",
            " select * from cdz_audit_record where is_valid=1 and join_id=#{contentId} order by last_command_time asc ",
            "</script>"
    })
    List<CdzAuditRecord> selectNewsAuditRecord(@Param("contentId") Integer contentId);

    /**
     * 修改文章状态
     *
     * @param id
     */
    @Update({
            " update  dt_news set is_valid = 0 where id = #{id}"
    })
    void updateArticleStatus(Integer id);

    /**
     * 新增文章
     *
     * @param wxArticleVo
     */
    @Insert({
            "<script> ",
            " INSERT INTO " +
                    "`dt_news` (" +
                    "`cid`, `title`, `content`, `create_time_new`, `paixu`, `from_to_area_id`, `img`,`create_time` , thumb , new_type , content_type , system_type_id , create_user_id" +
                    " , media_id ",
            "   )  " +
                    "VALUES (  " +
                    "           #{cid},#{title},#{content},#{createTimeNew},#{paixu},#{fromToAreaId},#{img},#{createTime} , #{thumb} , #{newType} ,#{contentType} ,#{systemTypeId},#{createUserId} " +
                    " ,#{mediaId} ",
            "       ); ",
            "</script> ",
    })
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void addArticle(WxArticleVo wxArticleVo);

    /**
     * 编辑文章
     *
     * @param dtNewsReq
     */
    @Update({
            " update dt_news set ",
            " title = #{title} , content = #{content} , ",
            " img = #{img} , from_to_area_id = #{fromToAreaId} ,",
            " thumb = #{thumb},system_type_id = #{systemTypeId} , create_time_new=#{createTimeNew},media_id=#{mediaId}  ",
            " where id = #{id}  ",
    })
    void updateArticle(DtNewsVo dtNewsReq);


    /**
     * 回显文章
     *
     * @param findArticleReq
     * @return
     */
    @Select({
            " SELECT  " +
                    "  an.id,   " +
                    "  an.cid,   " +
                    "  an.title,   " +
                    "  an.content,   " +
                    "  an.paixu,   " +
                    "  an.img," +
                    "  an.create_time_new," +
                    "  an.article_type  " +
                    "FROM  " +
                    "  dt_news AS an  " +
                    "WHERE  " +
                    "  an.id = #{id}  and is_valid = 1  "
    })
    ArticleVo findArticleById(FindArticleReq findArticleReq);

    /**
     * 通过类型查找索引
     *
     * @param type
     * @return
     */
    @Select({
            " select * from axh_wx_news_index where new_type = #{type} "
    })
    AxhWxNewsIndex findWxNewsIndexByType(@Param("type") String type);

    /**
     * 新增微信文章索引
     *
     * @param axhWxNewsIndex
     */
    @Insert({
            " INSERT INTO axh_wx_news_index (`new_type`, `new_index` )  " +
                    "VALUES  " +
                    " ( #{newType},#{newIndex})  "
    })
    void addWxNewsIndex(AxhWxNewsIndex axhWxNewsIndex);

    /**
     * 修改微信文章索引
     *
     * @param axhWxNewsIndex
     */
    @Update({
            " update  axh_wx_news_index set `new_index` = #{newIndex} where `new_type` = #{newType}"
    })
    void updateWxNewsIndexByType(AxhWxNewsIndex axhWxNewsIndex);

    /**
     * 批量插入
     *
     * @param articleVos
     */
    @Insert({
            " <script> INSERT INTO `aixihu`.`dt_news` " +
                    " (`title`, `thumb`, `content`, `create_time_new`, `create_time`, `status`,  " +
                    "                                `paixu`, `img`, `new_type`, `is_visible`, `media_id`, `original_url`," +
                    "                                   `wx_new_json`  )  " +
                    " VALUES " +
                    " <foreach collection = 'articleVos' index = 'index' item='item' separator = ',' >" +
                    " (  " +
                    "           #{item.title},#{item.thumb},#{item.content},#{item.createTimeNew}," +
                    "#{item.createTime},#{item.status},#{item.paixu},#{item.img},#{item.newType}," +
                    "#{item.isVisible},#{item.mediaId},#{item.originalUrl},#{item.wxNewJson}  " +
                    "       ) " +
                    " </foreach> " +
                    " </script> "
    })
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void batchAddWxArticle(@Param("articleVos") List<WxArticleVo> articleVos);

    /**
     * 从数据库查询微信文章列表
     *
     * @param findWxArticlesReq
     * @return
     */
    @Select({
            " <script>" +
                    " SELECT   " +
                    "   dn.id,    " +
                    "   dn.cid,    " +
                    "   dn.title,    " +
                    "   dn.thumb,    " +
                    "   dn.content,    " +
                    "   dn.create_time_new,    " +
                    "   dn.`status`,    " +
                    "   dn.paixu,    " +
                    "   dn.from_to_area_id,    " +
                    "   dn.img,    " +
                    "   dn.is_visible ," +
                    "   dn.original_url," +
                    "   dns.name classifyName  " +
                    "FROM   " +
                    "   dt_news AS dn " +
                    "   left join dt_news_sort dns on dn.cid = dns.id " +
                    " where dn.new_type = 2 " +
                    " <if test='title != null' >" +
                    " and title like '%${title}%' " +
                    " </if> " +
                    " <if test='isVisible != null' >" +
                    " and is_visible = #{isVisible} " +
                    " </if> order by cast( dn.paixu AS DECIMAL ) desc " +
                    " limit #{startIndex} , #{limit} " +
                    "</script> "
    })
    List<WxArticleVo> findWxArticles(FindWxArticlesReq findWxArticlesReq);

    /**
     * 获取微信文章总条数
     *
     * @param findWxArticlesReq
     * @return
     */
    @Select({
            " <script>" +
                    " SELECT   " +
                    "   count(dn.id)    " +
                    "FROM   " +
                    "   dt_news AS dn " +
                    " where new_type = 2  " +
                    " <if test='title != null' >" +
                    " and title like '%${title}%' " +
                    " </if> " +
                    " <if test='isVisible != null' >" +
                    " and is_visible = #{isVisible} " +
                    " </if>  " +
                    "</script> "
    })
    Integer findWxArticlesTotal(FindWxArticlesReq findWxArticlesReq);

    /**
     * 通过微信文章id查询微信文章的展示状态
     *
     * @param id
     * @return
     */
    @Select({
            " select * from dt_news where id = #{id} and is_valid = 1 "
    })
    WxArticleVo findWxArticleInfo(@Param("id") Integer id);

    /**
     * 修改微信文章的展示状态
     *
     * @param wxArticleVo
     */
    @Update({
            "  update dt_news set is_visible = #{isVisible}  where id = #{id} "
    })
    void updateWxArticlesIsVisible(WxArticleVo wxArticleVo);

    /**
     * 修改微信文章所属社区
     *
     * @param wxArticleVo
     */
    @Update({
            " update dt_news set cid = #{cid} , from_to_area_id = #{fromToAreaId}, status = 0  where id = #{id} "
    })
    void updateWxArticleArea(WxArticleVo wxArticleVo);


    /**
     * 逻辑删除文章
     *
     * @param removeArticleAreaReq
     */
    @Update({
            " update dt_news set is_valid = 0 where id = #{id} "
    })
    void removeArticleArea(RemoveArticleAreaReq removeArticleAreaReq);

    /**
     * 获取三级分类id
     *
     * @param threeClassifyId
     * @return
     */
    @Select({
            " select GROUP_CONCAT(id) ids from dt_news_sort where pid = #{threeClassifyId} and is_valid = 1"
    })
    String findThreeIds(@Param("threeClassifyId") Integer threeClassifyId);


    /**
     * 获取二级分类
     *
     * @param oneClassifyId
     * @return
     */
    @Select({
            " select id from dt_news_sort where pid = #{oneClassifyId}  and is_valid = 1 "
    })
    List<Integer> findTwoIds(@Param("oneClassifyId") Integer oneClassifyId);

    /**
     * 获取最大的排序
     *
     * @return
     */
    @Select({
            " select max( CAST(paixu as DECIMAL)) from  dt_news where is_valid = 1 "
    })
    Integer findCount();


    /**
     * 获取党政新闻列表
     *
     * @param findPartyPolicyNewsReq
     * @return
     */
    @Select({
            " <script>" +
                    " SELECT   " +
                    "   dn.id,    " +
                    "   dn.cid,    " +
                    "   dn.title,    " +
                    "   dn.thumb,    " +
                    "   dn.content,    " +
                    "   dn.create_time_new,    " +
                    "   dn.`status`,    " +
                    "   dn.paixu,    " +
                    "   dn.from_to_area_id,    " +
                    "   dn.img,    " +
                    "   dn.is_visible ," +
                    "   dn.original_url," +
                    "   dn.article_type," +
                    "   dns.name classifyName  " +
                    "FROM   " +
                    "   dt_news AS dn " +
                    "   left join dt_news_sort dns on dn.cid = dns.id " +
                    " where dn.cid = 122 and dn.is_valid = 1 and status = 1 " +
                    " order by cast( dn.paixu AS DECIMAL ) desc " +
                    "</script> "
    })
    List<WxArticleVo> findPartyPolicyNews(FindPartyPolicyNewsReq findPartyPolicyNewsReq);

    @Select({
            " SELECT  " +
                    "  GROUP_CONCAT( id ) ids   " +
                    "FROM  " +
                    "  dt_news_sort   " +
                    "WHERE  " +
                    "  pid in (${twoClassifyId})  " +
                    "  AND is_valid = 1 "
    })
    String findThreeIdsFun(@Param("twoClassifyId") String twoClassifyId);

    /**
     * 查询文章列表
     *
     * @param changeClassifyReq
     * @param id
     * @return
     */
    @Select({
            "<script> ",
            "SELECT dn.*,du.username,du.avatar,cst.name from ( ",
            " select * from dt_news where  ",
            " is_valid=1 and from_to_area_id in(${id}) and content_type=#{req.contentType} ",

            " <if test = 'req.title != null '> ",
            " and title like '%${req.title}%' ",
            " </if>",

            " <if test = 'req.systemTypeId != null '> ",
            " and system_type_id in (" ,
                "<foreach collection = 'req.systemTypeId ' index = 'index' item='item' separator = ',' > ",
                    "#{item} ",
                "</foreach> ",
            ") ",
            " </if>",

            " <if test = 'req.contentType == 2 '> ",
            " and (create_user_id=#{req.userId} or FIND_IN_SET(#{req.roleId},media_id))",
            " </if>",

            " <if test = 'req.auditStatus != null and req.auditStatus == 1 '> ",
            " and audit_status in(-1,101) ",
            " </if>",

            " <if test = 'req.auditStatus != null and req.auditStatus == 2 '> ",
            " and audit_status in(0) ",
            " </if>",

            " <if test = 'req.auditStatus != null and req.auditStatus == 3 '> ",
            " and audit_status in(100,99) ",
            " </if>",
            " ) dn",
            " left join dt_users du on dn.create_user_id = du.id ",
            " left join cdz_system_type cst on dn.system_type_id = cst.id ",
            " order by dn.create_time_new desc ",
            " limit #{req.startIndex},#{req.limit}",
            "</script> ",
    })
    List<DtNewsVo> findContentList(@Param("req") ChangeClassifyReq changeClassifyReq, @Param("id") String id);

    /**
     * 查询文章列表条数
     *
     * @param changeClassifyReq
     * @param id
     * @return
     */
    @Select({
            "<script>",
            "select   ",
            "   IFNULL(sum(if(audit_status=-1 or audit_status=101,1,0)),0) waitSubmitNumber,  ",
            "   IFNULL(sum(if(audit_status=0,1,0)),0) auditNumber,  ",
            "   IFNULL(sum(if(audit_status=100,1,0)),0) okNumber  ",
            "from dt_news where   ",
            "is_valid=1 and from_to_area_id in (${id})  and content_type=#{req.contentType} ",
            " <if test = 'req.title != null '> ",
            " and title like '%${req.title}%' ",
            " </if>",
            " <if test = 'req.systemTypeId != null '> ",
            " and system_type_id in (" ,
                "<foreach collection = 'req.systemTypeId ' index = 'index' item='item' separator = ',' > ",
                    "#{item} ",
                "</foreach> ",
            ") ",
            " </if>",
            " <if test = 'req.contentType == 2 '> ",
                " and (create_user_id=#{req.userId} or FIND_IN_SET(#{req.roleId},media_id))",
            " </if>",
            "</script>",
    })
    ArticleListResponse findContentListCount(@Param("req") ChangeClassifyReq changeClassifyReq, @Param("id") String id);

    /**
     * 查询昌东镇的文章类型
     *
     * @param pid
     * @return
     */
    @Select(" SELECT * FROM cdz_system_type WHERE is_valid = 1 and pid = #{pid} ")
    List<CdzSystemType> findCdzContentType(@Param("pid") Integer pid);

    /**
     * 修改公开状态
     *
     * @param contentId
     */
    @Update(" update dt_news set is_public = #{isPublic} where id = #{contentId} ")
    void auditContent(@Param("contentId") Integer contentId, @Param("isPublic") Integer isPublic);

    /**
     * 查询文章的公开状态
     *
     * @param contentId
     * @return
     */
    @Select(" select is_public from dt_news where id = #{contentId} ")
    Integer findPublic(@Param("contentId") Integer contentId);

    @Update({
            "<script>",
            " update dt_news set  audit_status = #{status} where id = #{contentId}",
            "</script>",
    })
    int updateNewsStatusById(@Param("contentId") Integer contentId, @Param("status") Integer status);

    @Select({
            "<script>",
            "  select * from dt_news where id = #{contentId}",
            "</script>",
    })
    DtNewsVo selectNewsById(@Param("contentId") Integer contentId);

    /**
     * 提交审核
     *
     * @param contentId
     */
    @Update(" update dt_news set audit_status = #{auditStatus} where id = #{contentId} ")
    void submitAudit(@Param("contentId") Integer contentId, @Param("auditStatus") Integer auditStatus);

    /**
     * 新增审核数据
     *
     * @param cdzAuditRecord
     */
    @Insert(" insert into cdz_audit_record (audit_type,join_id,submit_user_id,current_status,last_command_time,is_public) " +
            " values (#{cdz.auditType},#{cdz.joinId},#{cdz.submitUserId},#{cdz.currentStatus},#{cdz.lastCommandTime},#{cdz.isPublic} )")
    void insertRecord(@Param("cdz") CdzAuditRecord cdzAuditRecord);

    /**
     * 根据条件查询文章条数
     *
     * @param type
     * @return
     */
    @Select(" <script>" +
            "  select count(id) from dt_news where from_to_area_id in (${areaId}) and content_type = #{type} and is_valid = 1 " +
            " <if test = 'auditStatus != null and auditStatus != -1'>" +
            " and audit_status = #{auditStatus} " +
            "</if>" +
            " <if test = 'auditStatus == -1'>" +
            " and (audit_status = 2 or audit_status = -1) " +
            "</if>" +
            "</script>  ")
    Integer findContentAuditStatusCount(@Param("type") Integer type, @Param("auditStatus") Integer auditStatus, @Param("areaId") String areaId);

    /**
     * 插入数据
     *
     * @param axhFinanceLog
     */
    @Insert(" insert into axh_finance_log (finance_id,create_time,command_user_id,log_content,current_finance_status,log_type,remark) " +
            " values (#{log.financeId},now(),#{log.commandUserId},#{log.logContent},#{log.currentFinanceStatus},#{log.logType},#{log.remark} )")
    void insertFinance(@Param("log") AxhFinanceLog axhFinanceLog);

    /**
     * 修改状态
     *
     * @param isPublic
     * @param contentId
     */
    @Update("<script>" +
            " update axh_finance_log set " +
            "<if test = 'i==1'>" +
            " current_finance_status = #{isPublic} " +
            "</if> " +
            "<if test = 'i==0'>" +
            " current_finance_status = 0 " +
            "</if> " +
            " where finance_id = #{contentId}" +
            "</script>")
    void updateFinance(@Param("isPublic") Integer isPublic, @Param("contentId") Integer contentId, @Param("i") Integer i);

    @Update("UPDATE dt_news SET audit_status = #{status} , fail_msg = #{msg} WHERE id = #{id}")
    void updateAuditStatus(@Param("id") Integer newsId, @Param("status") Integer status, @Param("msg") String failMsg);

    /**
     * 查询需要审核的总条数
     */
    @Select("<script>" +
            " SELECT COUNT(id) FROM dt_news WHERE is_valid = 1 AND from_to_area_id IN (${fromAreaIds}) AND content_type IN (${req.contentType}) " +
            " AND audit_status = 0" +
            " <if test = 'req.title != null '> " +
            " and title like '%${req.title}%' " +
            " </if>" +
            " <if test = 'req.monthTime != null '> " +
            " and DATE_FORMAT( create_time_new,'%Y-%m')=#{req.monthTime}" +
            " </if>" +
            " <if test = 'systemTypeIds != null and systemTypeIds!=\"\" '> " +
            "  and system_type_id in (${systemTypeIds})" +
            " </if>" +
            " and (media_id is null or FIND_IN_SET(#{roleId},media_id)) " +
            "</script>")
    Integer findAuditTableCount(@Param("req") NewsDto changeClassifyReq, @Param("fromAreaIds") String fromAreaIds, @Param("systemTypeIds") String systemTypeIds, @Param("roleId") Integer roleId);

    @Select("<script>" +
            "  SELECT COUNT( DISTINCT a1.id) FROM (" +
            " SELECT * FROM dt_news WHERE is_valid = 1 AND from_to_area_id IN (${fromAreaIds}) AND content_type IN (${req.contentType}) " +
            " <if test = 'req.title != null '> " +
            " and title like '%${req.title}%' " +
            " </if>" +
            " <if test = 'req.monthTime != null '> " +
            " and DATE_FORMAT( create_time_new,'%Y-%m')=#{req.monthTime}" +
            " </if>" +
            " <if test = 'systemTypeIds != null and systemTypeIds!=\"\" '> " +
            "  and system_type_id in (${systemTypeIds})" +
            " </if>" +
            " and (media_id is null or FIND_IN_SET(#{roleId},media_id)) " +
            " ) a1 LEFT JOIN cdz_audit_record car ON a1.id = car.join_id " +
            " WHERE car.submit_user_id = #{userId} " +
            "</script>")
    Integer findAuditTableCount2(@Param("req") NewsDto changeClassifyReq, @Param("fromAreaIds") String fromAreaIds, @Param("systemTypeIds") String systemTypeIds, @Param("roleId") Integer roleId,
                                 @Param("userId") Integer id);

    @Select("<script>" +
            " SELECT dn.*,du.username,du.avatar,cst.name FROM (" +
            " SELECT * FROM dt_news WHERE is_valid = 1 AND from_to_area_id IN (${fromAreaIds}) AND content_type IN (${req.contentType}) " +
            " AND audit_status = 0 " +
            " <if test = 'req.title != null '> " +
            " and title like '%${req.title}%' " +
            " </if>" +
            " <if test = 'req.monthTime != null '> " +
            " and DATE_FORMAT( create_time_new,'%Y-%m')=#{req.monthTime}" +
            " </if>" +
            " <if test = 'systemTypeIds != null and systemTypeIds!=\"\" '> " +
            "  and system_type_id in (${systemTypeIds})" +
            " </if>" +
            " and (media_id is null or FIND_IN_SET(#{roleId},media_id)) " +
            " ) dn " +
            " left join dt_users du on dn.create_user_id = du.id " +
            " left join cdz_system_type cst on dn.system_type_id = cst.id " +
            " order by dn.create_time desc " +
            " limit #{req.startIndex},#{req.limit}" +
            "</script>")
    List<DtNewsVo> findAuditTable(@Param("req") NewsDto changeClassifyReq, @Param("fromAreaIds") String fromAreaIds, @Param("systemTypeIds") String systemTypeIds, @Param("roleId") Integer roleId);

    @Select("<script>" +
            " SELECT a1.* FROM ( " +
            " SELECT dn.*,du.username,du.avatar,cst.name FROM (" +
            " SELECT * FROM dt_news WHERE is_valid = 1 AND from_to_area_id IN (${fromAreaIds}) AND content_type IN (${req.contentType}) " +
            " <if test = 'req.title != null '> " +
            " and title like '%${req.title}%' " +
            " </if>" +
            " <if test = 'req.monthTime != null '> " +
            " and DATE_FORMAT( create_time_new,'%Y-%m')=#{req.monthTime}" +
            " </if>" +
            " <if test = 'systemTypeIds != null and systemTypeIds!=\"\" '> " +
            "  and system_type_id in (${systemTypeIds})" +
            " </if>" +
            " and (media_id is null or FIND_IN_SET(#{roleId},media_id)) " +
            " ) dn " +
            " left join dt_users du on dn.create_user_id = du.id " +
            " left join cdz_system_type cst on dn.system_type_id = cst.id " +
            " ) a1 LEFT JOIN cdz_audit_record car ON a1.id = car.join_id " +
            " WHERE car.submit_user_id = #{userId} " +
            " GROUP BY a1.id  " +
            " order by a1.create_time desc " +
            " limit #{req.startIndex},#{req.limit} " +
            "</script>")
    List<DtNewsVo> findAuditTable2(@Param("req") NewsDto changeClassifyReq, @Param("fromAreaIds") String fromAreaIds, @Param("systemTypeIds") String systemTypeIds, @Param("roleId") Integer roleId,
                                   @Param("userId") Integer id);
}
